#ifndef _S_NORFLASH_DRIVER_H_
#define _S_NORFLASH_DRIVER_H_

typedef enum
{
	norflash_OK = 0x00,
	norflash_powerdown_success,
	norflash_read_failed,
	norflash_write_failed,
	norflash_id_error,
	norflash_pagesize_error,
	norflash_size_not_matched,
	norflash_function_null_error,
	norflash_varify_failed,
	norflash_powerstandby_failed,
	norflash_powerdown_failed,
	norflash_waitready_failed,
}NorFlash_Result;

typedef struct __NorFlash_Handle
{
	unsigned char manId; 
	unsigned char devId;
	unsigned int devSize; // Flash total size: byte
	unsigned int pageSize; // Write page size
	unsigned int sectorSize; // Single sector size
	unsigned int sectorCount; // Sectors count, it should equal devSize / sectorSize
	unsigned int blockSize; // Block size, in winbond 25q16 it is 64kbyte

	void (*norflash_select)(void);
	void (*norflash_deselect)(void);
	NorFlash_Result (*norflash_write)(const unsigned char *wdata, unsigned short length);
	NorFlash_Result (*norflash_read)(unsigned char *rdata, unsigned short length);
}NorFlash_Handle;

#define FLASH_ADDRESS(page, offset)     (((page) << 12) + (offset))

extern NorFlash_Result S_NorFlash_VarifyInfo(NorFlash_Handle norflash, unsigned char *manId, unsigned char *devId);
extern NorFlash_Result S_NorFlash_Open(NorFlash_Handle norflash);
extern NorFlash_Result S_NorFlash_Close(NorFlash_Handle norflash);
extern NorFlash_Result S_NorFlash_Read(NorFlash_Handle norflash, unsigned int offset, unsigned char *buf, unsigned short length);
extern NorFlash_Result S_NorFlash_Write(NorFlash_Handle norflash, unsigned int offset, unsigned char *buf, unsigned short length);
extern NorFlash_Result S_NorFlash_Erase(NorFlash_Handle norflash, unsigned int offset, unsigned short length);
extern NorFlash_Result S_NorFlash_ChipErase(NorFlash_Handle norflash);

#endif
